package com.allwees.bs.c.module.user.listener;

import com.allwees.bs.c.common.auth.constant.AuthConstant;
import com.allwees.bs.c.module.ops.service.PointActiveDefUseService;
import com.allwees.bs.c.module.ops.service.UserCouponService;
import com.allwees.bs.c.module.user.dto.UserDto;
import com.allwees.bs.c.module.user.service.UserCashService;
import com.allwees.bs.c.module.user.service.UserPointService;
import com.allwees.core.common.util.freemarker.FreeMarkerUtil;
import com.allwees.sender.EMailSender;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yangylsky
 * @since 2019-08-16
 */
@Slf4j
public abstract class BaseUserRegisteredListener {

    @Autowired
    protected UserCouponService userCouponService;

    @Autowired
    protected UserPointService userPointService;

    @Autowired
    protected UserCashService userCashService;

    @Autowired
    private PointActiveDefUseService pointActiveDefUseService;

    @Autowired
    private EMailSender eMailSender;

    @Autowired
    protected FreeMarkerUtil freeMarkerUtil;

    protected void userRegistered(UserDto user,boolean showEmail) {
        long start = System.currentTimeMillis();
        createPointAndCashAccount(user.getUuid());
        log.info("===> createCouponAccount event end  [uuid:{}], time: {}ms", user.getUuid(), (System.currentTimeMillis() - start));
        inviterReward(user.getUuid());
        log.info("===> inviterAction event end [uuid:{}], time: {}ms", user.getUuid(), (System.currentTimeMillis() - start));
        sendEmail(user,showEmail);
    }

    private void sendEmail(UserDto user,boolean showEmail){
        try {
            String email = user.getEmail();
            if (StringUtils.isBlank(email)) {
                return;
            }
            if(email.contains("privaterelay.appleid.com")){
                return;
            }
            Map<String, Object> params = new HashMap<>(2);
            params.put("userName", user.getFirstName());
            params.put("show", showEmail);
            params.put("email", email);
            params.put("password", AuthConstant.DEFAULT_OAUTH_PASSWORD);

            String content = freeMarkerUtil.render("register_success_c.ftl", params);
            eMailSender.send(email, "Welcome to Allwees!", content, true);
        } catch (Exception e) {
            log.error("send email failed after registered successful.", e);
        }
    }

    /**
     * 创建积分账户 & 赠送积分 & 创建cash账户
     *
     * @param userUuid
     */
    private void createPointAndCashAccount(String userUuid) {
        try {
            userPointService.create(userUuid);
            pointActiveDefUseService.registered(userUuid);
            userCashService.create(userUuid);
        } catch (Exception e) {
            log.error("create point & cash account failed", e);
        }
    }

    /**
     * 给邀请人发放优惠券奖励
     *
     * @param userUuid
     */
    private void inviterReward(String userUuid) {
        try {
            userCouponService.inviteeRegistered(userUuid);
        } catch (Exception e) {
            log.error("inviter award action failed", e);
        }
    }
}
